Page({
  data: {
    selectedDate: '',
    selectedTime: '',
    availableTimes: ['10:00', '14:00', '18:00'],
    ticketQuantity: 1,
    ticketTypes: {
      adult: { name: '成人票', price: 100 },
      child: { name: '儿童票', price: 50 }
    },
    ticketTypeKeys: ['adult', 'child'],
    selectedTicket: 'adult', // 默认选中成人票
    totalPrice: 100,
  },

  // 日期选择
  onDateChange(e) {
    this.setData({ selectedDate: e.detail.value });
  },

  // 时间选择
  onTimeChange(e) {
    const selectedTime = this.data.availableTimes[e.detail.value];
    this.setData({ selectedTime });
  },

  // 票种选择
  onTicketChange(e) {
    const selectedTicket = e.detail.value; // 获取选中的票种 key
    const ticketPrice = this.data.ticketTypes[selectedTicket].price;

    // 更新选中的票种和总价
    this.setData({
      selectedTicket,
    });
    this.updatePrice(ticketPrice, this.data.ticketQuantity);
  },

  // 增加票数
  increaseQuantity() {
    let quantity = this.data.ticketQuantity;
    if (quantity < 10) {
      quantity += 1;
      const ticketPrice = this.data.ticketTypes[this.data.selectedTicket].price;
      this.updatePrice(ticketPrice, quantity);
    }
  },

  // 减少票数
  decreaseQuantity() {
    let quantity = this.data.ticketQuantity;
    if (quantity > 1) {
      quantity -= 1;
      const ticketPrice = this.data.ticketTypes[this.data.selectedTicket].price;
      this.updatePrice(ticketPrice, quantity);
    }
  },

  // 更新总价
  updatePrice(ticketPrice, quantity) {
    const totalPrice = ticketPrice * quantity;
    this.setData({
      ticketQuantity: quantity,
      totalPrice,
    });
  },

  // 提交预约
  submitReservation() {
    const { selectedDate, selectedTime, ticketQuantity, totalPrice, selectedTicket } = this.data;

    if (!selectedDate || !selectedTime) {
      wx.showToast({
        title: '请选择日期和时间',
        icon: 'none',
      });
      return;
    }

    wx.showModal({
      title: '确认预约',
      content: `日期：${selectedDate}\n时间：${selectedTime}\n票种：${this.data.ticketTypes[selectedTicket].name}\n数量：${ticketQuantity}\n总价：${totalPrice} 元`,
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '预约成功！',
            icon: 'success',
          });
        }
      },
    });
  },
});
